def is_prime(num):
    prime = num > 1 and (num % 2 != 0 or num == 2) and (num % 3 != 0 or num == 3)
    i = 5
    d = 2
    while prime and i * i <= num:
        prime = num % i != 0
        i += d
        d = 6 - d
    return prime


chek = input("Введите четырехзначное натуральное число")
number = list(chek)
answer = 0
if len(number) < 4:
    print(0)
else:
    for i in range(4):
        if is_prime(int(number[i])) and int(number[i]) > answer:
            answer = int(number[i])
    helper = ''
    for i in range(3):
        helper += number[i] + number[i+1]
        if is_prime(int(helper)) and int(helper) > answer:
            answer = int(helper)
        helper = ''
    for i in range(2):
        helper += number[i] + number[i + 1] + number[i + 2]
        if is_prime(int(helper)) and int(helper) > answer:
            answer = int(helper)
        helper = ''
    if is_prime(int(chek)) and int(chek) > answer:
        answer = int(chek)
    if answer == 0:
        print(0)
    else:
        
        print(answer)

        

